﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TV4Home.Server.TVEInteractionLibrary;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;

namespace TV4Home.WebInterface.UI.Controls
{
    public partial class TVGuideSingleChannelDetailsControl : System.Web.UI.UserControl
    {
        public int ChannelID { get; set; }
        public DateTime Date { get; set; }
        public int ProgramsCount { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void LoadData()
        {
            if (ChannelID == 0)
                return;

            List<WebProgramBasic> programsList = WebService.TV4HomeWebService.ServiceInstance.GetProgramsBasicForChannel(ChannelID, this.Date.Subtract(this.Date.TimeOfDay), this.Date.Subtract(this.Date.TimeOfDay).AddDays(1));

            this.ProgramsCount = programsList.Count;

            RepeaterChannelDetailsGuide.DataSource = programsList;
            RepeaterChannelDetailsGuide.DataBind();
        }

        protected void RepeaterChannelDetailsGuide_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Panel panel = e.Item.FindControl("singleProgramPanel") as Panel;

            AddProgramToPanel(panel, e.Item.DataItem as WebProgramBasic);
        }

        private void AddProgramToPanel(Panel singleProgramPanel, WebProgramBasic program)
        {
            Label time = new Label();
            time.Text = program.StartTime.ToShortTimeString() + @" - " + program.EndTime.ToShortTimeString() + @"&nbsp;&nbsp;"; //TODO: check for improvements
            time.Style.Add("float", "left");

            singleProgramPanel.Controls.Add(time);

            if (program.IdProgram != 0)
            {
                HyperLink descrText = new HyperLink() { Text = program.Title, CssClass = "TVGuideSingleChannelProgramText" };

                descrText.NavigateUrl = "~/ProgramInfoDialog.aspx?id=" + program.IdProgram;
                descrText.Attributes.Add("onClick", String.Format("showProgramInfoDialog('{0}'); return false;", program.IdProgram));

                singleProgramPanel.ID = "program" + program.IdProgram;

                singleProgramPanel.Controls.Add(descrText);
            }
            else  // no EPG
            {
                Label descrText = new Label() { Text = program.Title, CssClass = "TVGuideSingleChannelProgramText" };

                singleProgramPanel.Controls.Add(descrText);
            }
        }
    }
}